//
//  XBHomeController.m
//  Home
//
//  Created by 陈明茂 on 16/4/4.
//  Copyright © 2016年 iOS. All rights reserved.
//

#import "XBHomeController.h"
#import "XBZuiXinController.h"
#import "XBHome.h"

#define kNavBtnCount 3

typedef NS_ENUM(NSInteger, XBHomeSubController) {
    XBHomeSubControllerZuiXin = 0,
    XBHomeSubControllerPaiMai,
    XBHomeSubControllerGouWu
};

@interface XBHomeController ()<UICollectionViewDataSource,UICollectionViewDelegate>
/**
 *  导航条视图
 */
@property (weak, nonatomic) IBOutlet UIView *navBarView;
/**
 *  下划线
 */
@property (strong, nonatomic) UIView *underLineView;
/**
 *  上一个点击的按钮
 */
@property (strong, nonatomic) UIButton *preButton;
/**
 *  存放按钮的集合
 */
@property (strong, nonatomic) NSMutableArray *navBtns;

@property (assign, nonatomic) BOOL isClick;

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
// 模型数据
@property (strong, nonatomic) NSArray *homes;

// 控制器缓存池
@property (strong, nonatomic) NSMutableDictionary *vcCache;

@end

@implementation XBHomeController

- (void)viewDidLoad{
    
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;

    
    // 设置控制器的箭头
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    
    // 设置导航条的子视图
    [self setUpNavBarViewSubviews];
    


}
- (IBAction)leftBtnDidClick:(id)sender {
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"ZXXGroomCell" bundle:nil];
    
    UIViewController *vc = sb.instantiateInitialViewController;
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    // 设置布局
    self.layout.itemSize = self.collectionView.bounds.size;
    self.collectionView.bounces = NO;
    self.collectionView.pagingEnabled = YES;
    self.layout.minimumInteritemSpacing = 0;
    self.layout.minimumLineSpacing = 0;
    self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.showsHorizontalScrollIndicator = NO;
}


#pragma mark - 创建导航条视图的控件

- (void)setUpNavBarViewSubviews{
    
    self.navBtns = [NSMutableArray arrayWithCapacity:kNavBtnCount];
    // 设置按钮的大小
    CGFloat margin = 20;
    CGFloat btnH = self.navBarView.bounds.size.height - 8;
    CGFloat btnW = (self.navBarView.bounds.size.width - margin *(kNavBtnCount - 1)) / kNavBtnCount;
    for (NSInteger index = 0; index < kNavBtnCount; index ++) {
        // 创建按钮
        UIButton *button = [[UIButton alloc] init];
        [self.navBtns addObject:button];
        // 获得数据模型
        XBHome *home = self.homes[index];
        NSString *title = home.title;
        button.tag = index;
        // 监听事件
        [button addTarget:self action:@selector(navBarBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        [button setFont:[UIFont systemFontOfSize:16]];
        [self.navBarView addSubview:button];
        
        // 设置frame值
        CGFloat btnY = 0;
        CGFloat btnX = (margin + btnW) * index;
        button.frame = CGRectMake(btnX, btnY, btnW, btnH);
        // 初始化第一个按钮
        if (index == 0) {
            [self navBarBtnStatusChange:button];
        }
        
    }
    
    // 创建下划线
    UIView *underLineView = [[UIView alloc] init];
    underLineView.backgroundColor = [UIColor orangeColor];
    self.underLineView = underLineView;
    // 设置下划线的frame值
    CGFloat lineW = btnW;
    CGFloat lineH = 2;
    CGFloat lineX = 0;
    CGFloat lineY = btnH + 3;
    underLineView.frame = CGRectMake(lineX, lineY, lineW, lineH);
    [self.navBarView addSubview:underLineView];
}

#pragma mark - 导航按钮的点击
// 真正的按钮点击,触发滚动
- (void)navBarBtnDidClick:(UIButton *)btn{
    
    self.isClick = YES;
    [self navBarBtnStatusChange:btn];
        [self.collectionView setContentOffset:CGPointMake(self.collectionView.frame.size.width * btn.tag, 0) animated:YES];

    
}
// 按钮状态的改变,不触发滚动
- (void)navBarBtnStatusChange:(UIButton *)btn{
    self.preButton.selected = NO;
    btn.selected = YES;
    self.preButton = btn;
    
    // 设置下划线偏移值
    [UIView animateWithDuration:0.2 animations:^{
        
        CGRect frame = self.underLineView.frame;
        frame.origin.x = btn.frame.origin.x;
        self.underLineView.frame = frame;
    }];
}

#pragma mark - collectionView的代理方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return kNavBtnCount;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    // 获得模型数据
    XBHome *home = self.homes[indexPath.row];
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"home" forIndexPath:indexPath];
 
    UIViewController *vc = [self viewControllerFromCacheWithHome:home];
    
    [cell addSubview:vc.view];
    [self addChildViewController:vc];
    
    return cell;
}

#pragma mark - 从缓存池中加载控制器的view
- (UIViewController *)viewControllerFromCacheWithHome:(XBHome *)home{
    // 从缓存池中判断
    UIViewController *vc = [self.vcCache objectForKey:home.title];
    
    if (vc == nil) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:home.vcName bundle:nil];
        vc = sb.instantiateInitialViewController;
        
        [self.vcCache setObject:vc forKey:home.title];
    }

    return vc;
}

#pragma mark - scrollView的代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (self.isClick) {
        return;
    }
    CGFloat scrollW = scrollView.frame.size.width;
    // 获得当前的偏移值
    int offset = (int)(self.collectionView.contentOffset.x + scrollW * 0.5)/ scrollW;
    if (offset != self.preButton.tag) {
        // 获得当前的按钮
        UIButton *currentBtn = self.navBtns[offset];
        // 模拟点击
        [self navBarBtnStatusChange:currentBtn];
        
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
    self.isClick = NO;
}

#pragma mark - 懒加载数据

- (NSArray *)homes{
    
    if (_homes == nil) {
        _homes = [XBHome home];
    }
    return _homes;
}

- (NSMutableDictionary *)vcCache{
    
    if (_vcCache == nil) {
        _vcCache = [NSMutableDictionary dictionary];
    }
    return _vcCache;
}


@end
